//
// NetworkIO.h
//
// $Id: //base/include/TcpSocket.h
//
// Library: base
// Package: TcpSocket
// Module:  TcpSocket
//
// Definition of the TcpSocket class.
//
// Copyright (c) 2012-2012, iGuard.
// doitsjz@163.com	jelin
//


#ifndef TCPSOCKET_INCLUDED
#define TCPSOCKET_INCLUDED

#include <pjlib.h>

#include "BaseSocket.h"



/**
	simply tcp client and server
*/
class Proactor;
class BASE_API TcpSocket: public BaseSocket
{
public:
	enum
	{
		CONNECTOR = 1,
		ACCEPTOR
	};
	//for Acceptor when Client connect
	TcpSocket(Proactor* proactor,pj_sock_t newsock);
	//for Connector by Client
	TcpSocket(Proactor* proactor,char* remoteip=NULL,unsigned short remoteport=0);
	//for Acceptor by Server
	TcpSocket(Proactor* proactor,unsigned short listenport,int reuse=0);

	virtual ~TcpSocket();
public:
	int Connect();
	int Accept();
	int Send(const char* buff,long& size,int flags=0);
	int Recv(long size,int flags=0);
//	int Close();

public:
	virtual pj_bool_t OnDataRead(void *data,pj_size_t size,int status,pj_size_t* remainder);
	virtual pj_bool_t OnDataSend(pj_ioqueue_op_key_t *send_key,int sendsize){return true;};
	virtual pj_bool_t OnConnect(int status){return true;};
	virtual pj_bool_t OnAccept(Proactor* proactor,pj_sock_t newsock,const pj_sockaddr_t *src_addr,int src_addr_len){return true;};
	virtual void OnClose(int status){};

public:
	pj_bool_t OnAccept(pj_sock_t newsock,const pj_sockaddr_t *src_addr,int src_addr_len);
};


#endif